package Code.Polymorphsm.Demo2;

public class Test{
    public static void main(String[] args) {
        //创建对象（多态方式）
        //Fu f = new Zi();
        Animal a = new Dog();

        //调用成员变量：编译看左边，运行也看左边
        System.out.println(a.name);

        //调用成员方法：编译看左边，运行看右边
        a.show();

        //多态的弊端，不能调用子类的特有方法
        //类型强制转换
        if (a instanceof Dog dog){
            //Dog dog = (Dog) a;
            dog.lookHOME();
        }else if (a instanceof Cat cat){
            //Cat cat = (Cat) a;
            cat.catchMouse();
        }else {
            System.out.println("没有这个类型");
        }

    }
}

class Animal{
    String name = "动物";

    public void show(){
        System.out.println("Animal --- show方法");
    }

    public void eat(){
        System.out.println("动物在吃东西");
    }
}

class Dog extends Animal{
    String name = "狗";

    @Override
    public void show() {
        System.out.println("Dog --- show方法");
    }

    @Override
    public void eat() {
        System.out.println("狗吃骨头");
    }

    public void lookHOME(){
        System.out.println("狗看家");
    }
}


class Cat extends Animal{
    String name = "猫";

    @Override
    public void show() {
        System.out.println("Cat --- show方法");
    }

    @Override
    public void eat() {
        System.out.println("猫吃小鱼干");
    }

    public void catchMouse(){
        System.out.println("猫抓老鼠");
    }
}

